package manager.service;

import manager.dao.BaseCardDao;
import manager.domain.CardInfo;
import manager.factory.CardDaoFactory;

public class CardService {
//    通过学生库管工厂类，获取库管对象
    private BaseCardDao cardDao=CardDaoFactory.getCardDao();
//    private OtherCardDao cardDao=new OtherCardDao();
    public boolean addCard(CardInfo cardInfo) {
//        学生对象交给CardDao库管
//        CardDao cardDao=new CardDao();
//库管来找学生是否存在
        return cardDao.addCard(cardInfo);
    }

    public boolean isExists(String id) {
//        CardDao cardDao=new CardDao();
        CardInfo[] cardInfos =cardDao.findAllCard();
//        假设id不存在
        boolean exists=false;
//        遍历数组
        for (int i = 0; i < cardInfos.length; i++) {
            CardInfo cardInfo = cardInfos[i];
            if(cardInfo !=null&& cardInfo.getId().equals(id)){
                exists=true;
                break;
            }
        }
        return exists;
    }

    public void deleteCardById(String delId) {
        cardDao.deleteCardById(delId);
    }

    public CardInfo[] findAllCard() {
//        用CardDao获取学生数组
        CardInfo[] allCardInfo = cardDao.findAllCard();
        boolean flag=false;
        for (int i = 0; i < allCardInfo.length; i++) {
            CardInfo cardInfo = allCardInfo[i];
            if(cardInfo !=null){
                flag=true;
                break;
            }
        }
        if(flag==true){
            return allCardInfo;
        }else {
            return null;
        }
    }

    public void updateCard(String updateId, CardInfo newcard) {
        cardDao.updateCard(updateId,newcard);
    }
}